﻿using System;
using System.Web;
using System.Web.Caching;

/// <summary>
/// Summary description for CacheAbstraction
/// </summary>
public abstract class CacheAbstraction
{
	public CacheAbstraction()
	{
	}

    public abstract String GetCacheFilename();

    public virtual void RemoveCache()
    {
        CacheDependencyManager.RemoveCache(GetCacheFilename());
    }

    public virtual CacheDependency CreateDependency()
    {
        return CacheDependencyManager.CreateDependencyFile(GetCacheFilename());
    }

    public virtual CacheDependency CreateDependency(String sxIdentifier)
    {
        return CacheDependencyManager.CreateDependencyFile(GetCacheFilename(), sxIdentifier);
    }

    public virtual void AddToCache(String sxCacheKey, Object objCache, CacheType tpCache)
    {
        switch (tpCache)
        {
            case CacheType.FiveMinutes:
                HttpRuntime.Cache.Add(sxCacheKey, objCache, CreateDependency(),
                    Cache.NoAbsoluteExpiration,
                    TimeSpan.FromMinutes(5),
                    CacheItemPriority.Normal, null);
                break;
            case CacheType.OneHour:
                HttpRuntime.Cache.Add(sxCacheKey, objCache, CreateDependency(),
                    Cache.NoAbsoluteExpiration,
                    TimeSpan.FromHours(1),
                    CacheItemPriority.Normal, null);
                break;
            case CacheType.Daily:
                DateTime tomorrow = DateTime.Now.AddDays(1);
                HttpRuntime.Cache.Add(sxCacheKey, objCache, CreateDependency(),
                    new DateTime(tomorrow.Year, tomorrow.Month, tomorrow.Day, 0, 0, 1),
                    Cache.NoSlidingExpiration,
                    CacheItemPriority.Normal, null);
                break;
            default:
                HttpRuntime.Cache.Add(sxCacheKey, objCache, CreateDependency(),
                    Cache.NoAbsoluteExpiration,
                    TimeSpan.FromMinutes(15),
                    CacheItemPriority.Normal, null);
                break;
        }
    }
}
